﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;


namespace ComHelper
{
    class OpPrivateProfiles
    {
        //ini 文件路径
        public string path;

        /**
         * 引入动态链接库win32
         */
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section,
            string key,string def,StringBuilder retVal ,int size, string filePath);

        public OpPrivateProfiles(string iniPath)
        {
            path = iniPath;
        }

        /**
         * 自定义函数调用库函数
         */

        public void iniWriteFile(string section,
            string key, string val)
        {
            WritePrivateProfileString(section, key, val, this.path);
        }

        public string iniReadFile(string section,string key)
        {
            StringBuilder temp = new StringBuilder(255);
            long i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
            return temp.ToString();
        }
    }
}
